package com.feiyu.admin.controller;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.feiyu.admin.entity.ShopEntity;
import com.feiyu.admin.service.ShopService;
import com.feiyu.admin.service.openFeign.CommonFeignService;
import com.feiyu.admin.entity.AddressEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.*;

import com.feiyu.admin.entity.CheckShopEntity;
import com.feiyu.admin.service.CheckShopService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-08 22:35:19
 */
@RestController
public class CheckShopController {
    @Autowired
    private CheckShopService checkShopService;

    @Autowired
    private CommonFeignService commonFeignService;

    @Autowired
    private ShopService shopService;

    /**
     * 店铺审核订单带参查询
     */
    @RequestMapping("/searchCheckShopByParam")
    public R searchCheckShopByParam(@RequestBody Map<String, Object> params) {
        PageUtils checkShopInfos = checkShopService.queryPage(params);
        List<?> list = checkShopInfos.getList();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("totalCount", checkShopInfos.getTotalCount());
        hashMap.put("pageSize", checkShopInfos.getPageSize());
        hashMap.put("totalPage", checkShopInfos.getTotalPage());
        hashMap.put("currPage", checkShopInfos.getCurrPage());
        List<Map<String, Object>> checkShopInfos_list = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, Object> checkShopInfo_map = new HashMap<>();
            CheckShopEntity checkShopInfo = (CheckShopEntity) list.get(i);
            AddressEntity addressEntity = new AddressEntity();
            if (checkShopInfo.getShopAddress() == null) {
                return R.error("操作失败").put("data", null);
            }
            addressEntity.setAddressId(checkShopInfo.getShopAddress());
            R address = commonFeignService.searchAddressById(addressEntity);
            Object address_data = address.get("data");
            Map address_data_map = (Map) JSON.toJSON(address_data);
            checkShopInfo_map.put("checkShopId", checkShopInfo.getCheckShopId());
            checkShopInfo_map.put("shopId", checkShopInfo.getShopId());
            checkShopInfo_map.put("shopName", checkShopInfo.getShopName());
            checkShopInfo_map.put("shopAvator", checkShopInfo.getShopAvator());
            checkShopInfo_map.put("shopLicense", checkShopInfo.getShopLicense());
            checkShopInfo_map.put("shopIntro", checkShopInfo.getShopIntro());
            checkShopInfo_map.put("shopMain", checkShopInfo.getShopMain());
            checkShopInfo_map.put("shopType", checkShopInfo.getShopType());
            checkShopInfo_map.put("shopAddress", address_data_map.get("recipientAdd"));
            checkShopInfo_map.put("checkShopApplyTime", checkShopInfo.getCheckShopApplyTime());
            checkShopInfo_map.put("checkStats", checkShopInfo.getCheckState());
            checkShopInfo_map.put("checkUserId", checkShopInfo.getCheckUserId());
            checkShopInfo_map.put("checkTime", checkShopInfo.getCheckTime());
            checkShopInfo_map.put("checkRe", checkShopInfo.getCheckRe());
            checkShopInfos_list.add(checkShopInfo_map);
        }
        hashMap.put("list", checkShopInfos_list);
        return R.ok("成功").put("page", hashMap);
    }


    /**
     * 店铺审核订单精确查询
     */
    @PostMapping("/searchCheckShopById")
    public R searchCheckShopById(@RequestBody CheckShopEntity checkShopEntity) {
        CheckShopEntity checkShop = checkShopService.getById(checkShopEntity.getCheckShopId());
        HashMap<String, Object> map = new HashMap<>();
        BeanMap beanMap = BeanMap.create(checkShop);
        for (Object key : beanMap.keySet()) {
            map.put(key.toString(), beanMap.get(key));
        }
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setAddressId(checkShop.getShopAddress());
        R address = commonFeignService.searchAddressById(addressEntity);
        Object address_data = address.get("data");
        Map address_data_map = (Map) JSON.toJSON(address_data);
        map.remove("delFlag");
        map.put("shopAddress", address_data_map.get("recipientAdd"));
        return R.ok("成功").put("data", map);
    }

    /**
     * 店铺审核订单审核处理
     */
    @PostMapping("/checkShop")
    //@RequiresPermissions("product:checkshop:update")
    public R checkShop(@RequestBody CheckShopEntity checkShop,
                       HttpServletRequest request) {
        String userId = request.getHeader("userId");
        checkShop.setCheckUserId(Integer.parseInt(userId));
        checkShopService.updateById(checkShop);
        if (checkShop.getCheckState() == 1) {
            CheckShopEntity checkShopEntity = checkShopService.getById(checkShop.getCheckShopId());
            ShopEntity shopEntity = new ShopEntity();
            shopEntity.setShopId(checkShopEntity.getShopId());
            shopEntity.setShopName(checkShopEntity.getShopName());
            shopEntity.setShopAvator(checkShopEntity.getShopAvator());
            shopEntity.setShopLicense(checkShopEntity.getShopLicense());
            shopEntity.setShopIntro(checkShopEntity.getShopIntro());
            shopEntity.setShopMain(checkShopEntity.getShopMain());
            shopEntity.setShopType(checkShopEntity.getShopType());
            shopEntity.setShopAddress(checkShopEntity.getShopAddress());
            shopService.updateById(shopEntity);
        }
        return R.ok("成功").put("data", null);
    }
}
